#pragma once 
#include<iostream>
#include<string>
#include<unistd.h>
using namespace std;
#include<pthread.h>
class Task
{
  private:
    int sock;
    string ip;
    int port;
  public:
    Task(){}
    Task(int  _sock,string _ip,int _port)
    :sock(_sock)
     ,ip(_ip)
     ,port(_port)
    {}
    void run()
    {
      auto f = [](int sock,string ip,int port)
        {
      char buffer[1024];
      while(true)
      {
        ssize_t size=read(sock,buffer,sizeof(buffer)-1);//阻塞读
        if(size>0)
        {
          buffer[size]=0;
          cout<<"receive "<<ip<<":"<<port<<"#"<<buffer<<endl;
          //TCP是全双工的读写都是一个文件描述符
          write(sock,buffer,size);
        }
        else if(size==0)//对端关闭
        {
          cout<<ip<<":"<<port<<"close!"<<endl;
          break;
        }
        else 
        {
          cerr<<sock<<"read error"<<endl;
          break;
        }
      }
      close(sock);
        };
      f(sock,ip,port);
    }
};
